package com.axend.vetwavve.adapter

import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.axend.lib_base.ext.roundToFloatOneDecimal
import com.axend.lib_base.ext.toFloatOrDefault
import com.axend.lib_base.ext.toPlainStringNoRounding
import com.axend.lib_room.entity.PetInfoEntity
import com.axend.lib_service.Constant
import com.axend.vetwavve.R
import com.axend.vetwavve.databinding.AdapterPetRecordItemBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder

/**
 * @description 宠物档案列表适配
 * @author admins
 * @time 2024/6/11 17:40
 */
class PetRecordAdapter :
    BaseQuickAdapter<PetInfoEntity, BaseDataBindingHolder<AdapterPetRecordItemBinding>>(
        R.layout.adapter_pet_record_item
    ) {

    private var checkedMap: MutableMap<Int, PetInfoEntity> = mutableMapOf()

    init {
        addChildClickViewIds(R.id.iv_select)
    }

    override fun convert(
        holder: BaseDataBindingHolder<AdapterPetRecordItemBinding>,
        item: PetInfoEntity,
    ) {
        holder.dataBinding?.apply {
            tvPetName.text = item.petNickName
            tvPetAge.text = item.age.toString()
            tvPetMaster.text = item.masterName
            tvPetBreed.text = item.breed
            tvPetSex.text =
                context.resources.getStringArray(com.axend.lib_base.R.array.app_sex_type)[item.sex]
            tvPetSpecies.text =
                context.resources.getStringArray(com.axend.lib_base.R.array.app_pet_types)[item.species]
            tvPetWeight.text = "${item.weight.toPlainStringNoRounding()} ${context.resources.getStringArray(com.axend.lib_base.R.array.app_weight_unit)[item.weightUnit]}"
            ivHeaderImg.setImageResource(if (item.species == 0) com.axend.lib_base.R.mipmap.img_dog else com.axend.lib_base.R.mipmap.img_cat)
            ivSelect.visibility = if (item.showSelect) View.VISIBLE else View.GONE
            if (item.isSelect) {
                ivSelect.setImageResource(com.axend.lib_base.R.mipmap.icon_xuanzhong)
            } else {
                ivSelect.setImageResource(com.axend.lib_base.R.mipmap.img_weixuanzhong)
            }
        }

    }

    /**
     * 局部刷新方法
     */
    override fun convert(
        holder: BaseDataBindingHolder<AdapterPetRecordItemBinding>,
        item: PetInfoEntity,
        payloads: List<Any>
    ) {
        //局部刷新
        for (playObj in payloads) {
            if (playObj == Constant.PLAY_LOAD_REFRESH_SELECT) {
                if (item.isSelect) {
                    holder.dataBinding?.ivSelect?.setImageResource(com.axend.lib_base.R.mipmap.icon_xuanzhong)
                } else {
                    holder.dataBinding?.ivSelect?.setImageResource(com.axend.lib_base.R.mipmap.img_weixuanzhong)
                }
            }
        }
    }


    /**
     * 刷新复选框是否显示状态
     * mShowSelect:是否显示
     */
    fun refreshShowChecked(mShowSelect: Boolean) {
        data.forEach {
            it.showSelect = mShowSelect
        }
        notifyDataSetChanged()
    }


    /**
     * 刷新复选并返回选中项
     */
    fun refreshItemSelect(position: Int, mSelect: Boolean): MutableMap<Int, PetInfoEntity> {
        data[position].isSelect = mSelect
        if (mSelect) {
            checkedMap[position] = data[position]
        } else {
            checkedMap.remove(position)
        }
        notifyItemChanged(position, Constant.PLAY_LOAD_REFRESH_SELECT)
        return checkedMap
    }

    /**
     * 是否是全选
     */
    fun isSelectedAll(): Boolean = data.size == checkedMap.size

    /**
     * 获取选中的checkMap
     */
    fun getCheckedMap(): MutableMap<Int, PetInfoEntity> = checkedMap


    /**
     * 选中所有项
     */
    fun selectAll(mSelectAll: Boolean) {
        checkedMap.clear()
        data.forEachIndexed { index, petInfoEntity ->
            petInfoEntity.isSelect = mSelectAll
            if (mSelectAll) {
                checkedMap[index] = data[index]
            } else {
                checkedMap.remove(index)
            }
            notifyItemChanged(index, Constant.PLAY_LOAD_REFRESH_SELECT)
        }
    }


    /**
     * 添加尾部条数显示
     */
    fun showCountFooterView(numberStr: String = data.size.toString(), isShow: Boolean) {
        if (!isShow || numberStr == "0") {
            removeAllFooterView()
            return
        }
        val view = View.inflate(context, R.layout.adapter_pet_record_footer, null)
        var tvCount: TextView = view.findViewById<View>(R.id.tv_count_number) as TextView
        val ssb = SpannableStringBuilder(if (numberStr.toFloatOrDefault(0f)>1)
            context.getString(com.axend.lib_base.R.string.app_result_found) else context.getString(
            com.axend.lib_base.R.string.app_results_found))
        ssb.insert(1, numberStr)
        ssb.setSpan(
            ForegroundColorSpan(
                ContextCompat.getColor(
                    context,
                    com.axend.lib_base.R.color.colorPrimary
                )
            ),
            1,
            1 + numberStr.length,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        tvCount.text = ssb
        setFooterView(view)
    }


}